(function (global){
	var CGI = {
		NOTICE_MAIN: function (){return window.frontPath + 'notice/pagingPageContent?catalogId=4&pageIndex=1&pageSize=8&r='+new Date().getTime()},//查询系统公告
		LITTLE_LOTTERY: function (){return window.frontPath + 'lottery/queryLittleLottery?r='+new Date().getTime()},//查询少量彩种
		QUERY_AWARD_LIST: function (){return window.frontPath + 'home/queryAwardList?r=' + new Date().getTime()}//查询奖品列表
	};
	
	var home = {
		/**
		 * 系统公告
		 */
		notice : {
			//事件绑定
			__eventBind : function (){
				
			},
			//请求
			__ajaxSender : function (){
				$.ajax({
					url 	: CGI.NOTICE_MAIN(),
					method 	: 'GET',
					success : function(data){
						if(data.retcode == 0){
							$('#home-notice').html(_.template($('#home-notice-template').html() , 
								{items : data.data.list})).
								find('li').hover(function (){
									$(this).addClass('over');
								},function (){
									$(this).removeClass('over')
								}).end().find('li:odd').addClass('alt');
							$('#home-notice a[data-flag="detail"]').each(function (i,ele){
								ele.href = window.frontPath + '#/notice/detail/'+$(this).
									parent().find('[data-flag="id"]').val();
							});
						}else{
							$.exception(data.retcode,data.retmsg);
						}
					}
				});
			},
			//初始化
			init : function (){
				this.__ajaxSender();
			}
		},
		
		/**
		 * 彩种查询与投注
		 */
		lottery : {
			data 		: null,				//服务器取回的数据，将它缓存
			//事件绑定
			__eventBind : function (){
				var _this = this;
				$('#tags').unbind('click').delegate('li' , 'click' , function (){
					$('#tags li').removeClass('selectTag');
					$(this).addClass('selectTag');
					var _id = +$(this).attr('data-id');

					var i = 0,dataL = _this.data.length;
					for(var i =0;i<dataL;i++){
						var _obj = _this.data[i];
						if(_obj['id'] === _id){
							$('#tagContent').html(_.template(
								$('#home-little-lottery-content').html() , _obj));

							if($('#id-little-lottery')[0]){
								$('#id-little-lottery').html('');
							}else{
								$('<div>' ,{
									id: 'id-little-lottery',
									'style':'display:none;'
								}).appendTo($('body'));
							}
							$('#id-little-lottery').load($.substitute(
								'{frontPath}homedata/{_id}.html?{timestamp}' , {
								frontPath : window.frontPath,
								_id       : _id,
								timestamp : new Date().getTime()
							}),null , function (response , status , xhr){
								if(!isNaN(xhr.status)){
									var _status = +xhr.status;
									if(404 === _status){	//没有取到开奖数据
										$('.prize-img b').html('000000期');
										if(_obj.hash.indexOf('pk10') > -1){
											$('.prize-num').html('<b>--</b>\
											<b>--</b><b>--</b>\
											<b>--</b><b>--</b>\
											<b>--</b><b>--</b>\
											<b>--</b><b>--</b>\
											<b>--</b>');
										}else{
											$('.prize-num').html(
												'<b>--</b><b>--</b>\
												<b>--</b><b>--</b>\
												<b>--</b>');
										}
									}else if(200 === _status){
										$('.prize-img b').html($(
											'#id-little-lottery .prize-img b').html() + '期');
										$('.prize-num').html($(
											'#id-little-lottery .prize-num').html());
									}
								}
							});
							break;
						}
					}
				});
				$('#tags li:eq(0)').trigger('click');
				//换一注
				$('#tagContent').unbind('click').delegate(
					'[data-flag="change"]' , 'click' , function (){
					_this.init();
				});
			},
			//请求
			__ajaxSender : function (){
				var _this = this;
				$.ajax({
					url 	: CGI.LITTLE_LOTTERY(),
					method 	: 'GET',
					success : function(data){
						if(data.retcode == 0){
							_this.data = data.data;
							
							$('#tags').html(_.template($(
								'#home-little-lottery-title').html() , 
							{items : data.data}));
							
							_this.__eventBind();
						}else{
							$.exception(data.retcode,data.retmsg);
						}
					}
				});
			},
			//初始化
			init : function (){
				this.__ajaxSender();
			}
		},
		queryAwardList : function (){
			$.ajax({
				url 	: CGI.QUERY_AWARD_LIST(),
				method 	: 'GET',
				success : function(data){
					if(data.retcode == 0){
						data.data.length = 8;
						$('#award-list-area').html(
							_.template($('#home-award-list').html() , 
						{items : data.data})).
						find('li').hover(function (){
							$(this).addClass('over');

						},function (){
							$(this).removeClass('over')
						}).end().find('li:odd').addClass('alt');
					}else{
						$.exception(data.retcode,data.retmsg);
					}
				}
			});
		},

		init : function (){
			this.notice.init();
			this.lottery.init();
			this.queryAwardList();
		}
	};
	home.init();
})();